package pre.lxm.demo.chart.recycler;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import pre.lxm.demo.chart.bean.RecordBean;
import pre.lxm.demo.chart.recycler.RecordChartAdapter;


public class ChartDecoration extends RecyclerView.ItemDecoration {

    private Context mContext;
    private Paint mPaint;
    private static final String TAG = "ChartDecoration";
    private Drawable mDivider;
    public static final int[] ATRRS = new int[]{
            android.R.attr.listDivider
    };

    public ChartDecoration(Context context) {
        this.mContext = context;
        final TypedArray ta = context.obtainStyledAttributes(ATRRS);
        this.mDivider = ta.getDrawable(0);
        this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor("#ff0000"));
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void onDraw(Canvas mCanvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(mCanvas, parent, state);
        Log.e(TAG, "--onDraw--");
        int childSize = parent.getChildCount();
        RecordChartAdapter mAdapter = (RecordChartAdapter) parent.getAdapter();
        for (int i = 0; i < childSize; i++) {
            View childAt = parent.getChildAt(i);
            RecordBean itemData = mAdapter.getItemData(i);
            RecordChartAdapter.RecordChartVH childVH = (RecordChartAdapter.RecordChartVH) parent.getChildViewHolder(childAt);
            if (i < childSize - 1) {
                String itemResult = itemData.getResult();

                View nextView = parent.getChildAt(i + 1);
                RecordChartAdapter.RecordChartVH nextVH = (RecordChartAdapter.RecordChartVH) parent.getChildViewHolder(nextView);
                RecordBean nextRecordBean = mAdapter.getItemData(i + 1);
                String nextResult = nextRecordBean.getResult();
                if (itemResult.equals(nextResult)) {
                    Log.e(TAG, "--equals--" + itemResult);
                    //相同
                    Rect mRect = new Rect();
                    Rect nextRect = new Rect();
                    switch (itemResult) {
                        case "3":
                            //胜
                            childVH.winTv.getDrawingRect(mRect);
                            nextVH.winTv.getDrawingRect(nextRect);
                            break;
                        case "1":
                            //平
                            childVH.flatTv.getDrawingRect(mRect);
                            nextVH.flatTv.getDrawingRect(nextRect);
                            break;
                        case "0":
                            //负
                            childVH.failureTv.getDrawingRect(mRect);
                            nextVH.failureTv.getDrawingRect(nextRect);
                            break;
                    }
                    Log.e(TAG, "--nextRect--" + nextRect.left + "---" + nextRect.top + "---" +
                            nextRect.right + "---" + nextRect.bottom);
                    Log.e(TAG, "--rect--" + "--mRect.right--" + mRect.right + "--mRect.height()/2--"
                            + mRect.height() / 2 + "--nextRect.left--" + mRect.right + "--nextRect.height()--" + nextRect.height());
                    mDivider.setBounds(mRect.right, mRect.height() / 2, mRect.right + 10, nextRect.height
                            ());
                    mDivider.draw(mCanvas);

                }
            }

        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
            state) {
        super.getItemOffsets(outRect, view, parent, state);
//        outRect.set(0, 0, 0, 0);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
        int viewLayoutPosition = layoutParams.getViewLayoutPosition();
        if (viewLayoutPosition == parent.getChildCount()) {
            outRect.set(0, 0, 0, 0);
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
        }
    }
}
